Linkedin Java 認證題庫


Posted by c9103205 on 2021-07-02

class Main {
    public static void main(String[] args){
       String message = "Hello";
       for (int i = 0; i<message.length(); i++){
          System.out.print(message.charAt(i+1));
       }
    }
}
  1. "Hello"
  2. A runtime exception is thrown.
  3. The code does not compile.
  4. "ello"

answer : 2
解析:
這題是個小陷阱,也是新手工程師常犯的錯誤
message.length() == 5
但字串轉成 char 的 index 是從 0開始計算
也就是 "01234"
所以當(i+1)==5的時候,會出現
StringIndexOutOfBoundsException
不可不慎阿~~


#linkedin #java







Related Posts

Semantic segmentation 簡介

Semantic segmentation 簡介

Node.appendChild() vs. Element.append()

Node.appendChild() vs. Element.append()

[ 學習筆記系列 ] 很基礎的 JavaScript 入門 (二) - ES6 語法與 Module

[ 學習筆記系列 ] 很基礎的 JavaScript 入門 (二) - ES6 語法與 Module


Comments